package com.joejag.mavenstats.client.views.north;

import com.google.gwt.user.client.ui.*;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.ButtonConfig;
import com.gwtext.client.widgets.LayoutDialog;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.joejag.mavenstats.client.MavenStats;
import com.joejag.mavenstats.client.utils.ImageHyperlink;
import com.joejag.mavenstats.client.views.forms.project.ProjectDTODialog;
import com.joejag.mavenstats.client.views.forms.project.add.AddProjectDTOForm;

public class TopPanel extends Composite implements ClickListener
{
  private LayoutDialog dialog;

  public interface Images extends ImageBundle
  {
    /**
     * @gwt.resource com/joejag/mavenstats/client/images/logo.png
     */
    AbstractImagePrototype logo();
  }

  private HTML aboutLink = new HTML("<a href='javascript:;'>About</a>");

  public TopPanel(Images images)
  {
    HorizontalPanel outer = new HorizontalPanel();
    VerticalPanel inner = new VerticalPanel();

    outer.setHorizontalAlignment(HorizontalPanel.ALIGN_RIGHT);
    inner.setHorizontalAlignment(HorizontalPanel.ALIGN_RIGHT);

    HorizontalPanel links = new HorizontalPanel();
    links.setSpacing(4);
    links.add(aboutLink);

    addNewProjectButton(links);

    ImageHyperlink logo = new ImageHyperlink(images.logo().createImage(), new ImageHyperlink.ImageHyperlinkAction()
    {
      public void performAction()
      {
        MavenStats.instance().showHomepage();
      }
    });

    outer.add(logo);
    outer.setCellHorizontalAlignment(logo, HorizontalPanel.ALIGN_LEFT);

    outer.add(inner);
    inner.add(links);

    aboutLink.addClickListener(this);

    initWidget(outer);
    setStyleName("mail-TopPanel");
    links.setStyleName("mail-TopPanelLinks");
  }

  private void addNewProjectButton(HorizontalPanel buttonsPanel)
  {
    com.gwtext.client.widgets.Button editButton = new com.gwtext.client.widgets.Button(new ButtonConfig()
    {
      {
        setText("Add New Project");
      }
    });
    buttonsPanel.add(editButton);
    editButton.addButtonListener(new ButtonListenerAdapter()
    {
      public void onClick(Button button, EventObject eventObject)
      {
        super.onClick(button, eventObject);
        new ProjectDTODialog(new AddProjectDTOForm()).show(button.getEl());
      }
    });
  }

  public void onClick(Widget sender)
  {
    if (sender == aboutLink)
    {
      AboutDialog dlg = new AboutDialog();
      dlg.show();
      dlg.center();
    }
  }


}
